<?php
ini_set('memory_limit', '-1');
ini_set( 'max_execution_time ',   0);  
	require_once '../simple_html_dom.php';
	//require_once '../../plugins/class.mysql.php';
    require_once '../../plugins/db.php';
    //require_once '../../dragon/config.inc.php';


    
    /*
  	$db = new Db();
	$category = new FancaiCategory($db);
	$category->loadCategory();
	$category->loadVegetables(-1, array());
	$category->downVegetablesImg();
	*/
    
	/*
	$category->reWriterVegetables();
	$category->loadVegetables(30);
	
	$category->skipwhenExistsVegetables();
	$category->loadVegetables();
	*/
	//$category->plainVegetablesInfo('http://www.fancai.com/meishi/120415/', 11);
	
	function loadSingleCateVegetables($cateId){
		$db = new Db();
		$category = new FancaiCategory($db);
		
		$category->reWriterVegetables();
		$category->loadVegetables($cateId);
	}
	
	function loadVegetables($excludeArray){
		$db = new Db();
		$category = new FancaiCategory($db);
		//$category->skipwhenExistsVegetables();
		
		$category->loadVegetables(-1, $excludeArray);
	}
	
	function downloadVegetableImg(){
		$db = new Db();
		$category = new FancaiCategory($db);
		$category->downVegetablesImg();
	}
	
	class FancaiCategory{
		private $db;
		private $website = 'http://www.fancai.com';
		private $loadCount = 0;
		private $rewrite_vegetables = false;
		
		
		function FancaiCategory($db){
			$this->db = $db;
		}
		
		function loadCategory(){
			$topLevel = 'http://www.fancai.com/meishi';
			$html = file_get_html($topLevel);
	
			$tr  = $html->find('div[id^=trade_second]');
			
			$this->insertFancaiCate('菜谱大全', 0, 'http://www.fancai.com/meishi/');
			$parentId = $this->getParentNodeId('菜谱大全');
			
			foreach ($tr as $k=>$v){
				
				$topLevelNode = $v->find('h2[id=tradeBig]',0)->innertext;
				
				$this->insertFancaiCate($topLevelNode, $parentId);
				$index = 0;
				$vegetables = $v->find('div[id=tradeshow]',0)->find('li');
				$tmpParentId = $this->getParentNodeId($topLevelNode);
				foreach($vegetables as $vek=>$vev){
					$node = $vev->find('a',0)->innertext;
					//$parentId = $this->getParentNodeId($node);		
					$url = 'http://www.fancai.com'.$vev->find('a',0)->href;
					
					$this->insertFancaiCate($node, $tmpParentId, $url, $index, 1);
					$index++;
				}
				
			}
		}
		
		function downVegetablesImg(){
			//$sql = "select a.* from dragon_fancai_vegetables a where a.id not in (select b.veget_id from dragon_fancai_file b)";
			$sql = "select a.* from dragon_fancai_vegetables a ";
			$result = $this->db->fetchRows($sql);
			foreach($result as $k=>$v){
				if ($html){
					$html->clear();
					unset($html);
				}
				
				$html = file_get_dom($v['web_url']);
				$img = $html->find('div.fcd_titleimg', 0);
				if(!$img){
					continue;
				}
				$img = $img->find('img', 0);
				if(!$img){
					continue;
				}
				
				$src = $img->src;
				$this->downloadImg($v['id'], $src);
			}
		}
		
		function loadVegetables($cateId = -1, $excludeCate = array()){
			
			$sql = "select * from dragon_fancai_category where isleaf=1 and fetched='N' ";
			if($cateId > 0){
				$sql .= " and id=$cateId";
			}
			if (count($excludeCate) > 0){
				$sql .= " and id not in (";
				foreach ($excludeCate as $k=>$v){
					$sql .= $v.",";
				}
				$sql = substr($sql, 0, strlen($sql)-1);
				$sql .= ") ";
				
			}
			echo $sql;
			$cateList = $this->db->fetchRows($sql);
			
			foreach ($cateList as $k=>$v){
				$oldUrl = trim($v['old_url']);
				if(strlen($oldUrl) <= 2){
					continue;
				}
				
				$this->loopPlainVegetableInfoFromHtml($oldUrl, $v['id']);
				
				$sql = "update dragon_fancai_category set fetched='Y' where id=".$v['id'];
				$this->db->query($sql);
				unset($sql);
			}
		}
		
		function loopPlainVegetableInfoFromHtml($url, $cateId){
			$html = file_get_html($url);
				
			if(!$html){
				return false;
			}	
			
			$footerPage = $html->find('div[class=x_page]', 0);
			$nextPage = trim($footerPage->find('a[title=下一页]', 0)->href);
			
			
			$counter = 0;
			$vegetableList = $html->find('div[class=fc_list]',0)->find('ul li');
			foreach ($vegetableList as $k=>$v){
				$a = $v->find('div.fcl_titler a', 0);
				$tmpUrl = $this->website.$a->href;
				
				$counter++;
				
				if ($this->rewrite_vegetables && $this->isExistsVegetables($tmpUrl, $cateId)){
					continue;
				}
				
				$this->plainVegetablesInfo($tmpUrl, $cateId, $url);
				
			}
			
			$html->clear();
			unset($html);
			unset($vegetableList);

			if($counter <= 0){
				unset($counter);
				unset($nextPage);
				return true;	
			}
			
			if(isset($nextPage) && strlen($nextPage) > 2){
				
				$page = $this->getCurrentPage($nextPage);
				if ($page > 0 ){
					$this->loopPlainVegetableInfoFromHtml($this->website.$nextPage, $cateId);
				}
				   
			}
			
			unset($nextPage);
		}
		
		function plainVegetablesInfo($url, $cateId, $currentPageUrl){
			$html = file_get_html($url);
			
			$vegetableBlock = $html->find('div.fcd_center ',0);
			if (!$vegetableBlock)return;
			
			$vegetableTitle = $html->find('div.fcd_center ',0)->find('div.cn_l h1', 0)->innertext;
			$vegetableTitle = addslashes($vegetableTitle);
			
			$majorMaterial = ''; // 主料
			$slaveMaterial = ''; // 辅料
			$seasoning = ''; //调料
			
			foreach($vegetableBlock->find('div.stock div.stock_c dl') as $k=>$v){
				$p = $v->find('dt',0)->innertext;
				$val = '';
				foreach ($v->find('dd', 0)->find('li') as $lk=>$lv){
					$val .= trim($lv->plaintext).'&nbsp;';
				}
				$val = chop($val, " ");
				
				if (substr_count($p, '主料') > 0){
					$majorMaterial = $val;
				}
				elseif (substr_count($p, '辅料') > 0){
					$slaveMaterial = $val;
				}
				elseif (substr_count($p, '调料') > 0){
					$seasoning = $val;
				}
			}
			
			$majorMaterial = addslashes($majorMaterial);
			$slaveMaterial = addslashes($slaveMaterial);
			$seasoning = addslashes($seasoning);
			
			$cookingCnt = '';
			$cookingTip = '';
			
			foreach ($vegetableBlock->find('div.procedure') as $k=>$v){
				$tl = $v->find('h3', 0);
				if (!$tl) continue; //如果没有标题 则不是菜谱信息
				
				$tlText = $tl->plaintext;
				if(substr_count($tlText, '详细步聚') > 0){
					$cookingCnt = $v->find('div.procedure_c li', 0)->innertext;
				}
				elseif (substr_count($tlText, "制作提示") > 0){
					$cookingTip = $v->find('div.procedure_c li', 0)->innertext;
				}
			}
			
			$cookingCnt = addslashes($cookingCnt);
			$cookingTip = addslashes($cookingTip);
			
			$relatCnt = '';
			if ($vegetableBlock->find('div.cate_xk',0)){
				$relat = $vegetableBlock->find('div.cate_xk',0);
				if(substr_count($relat->find('h3', 0)->innertext, '故事') > 0){
					$relatCnt = $relat->find('div', 0)->innertext;
				}
			}
			
			$relatCnt = addslashes($relatCnt);
			
			$nutritionWorth = $vegetableBlock->find('div.pabulum li', 0)->innertext;
			$nutritionWorth = addslashes($nutritionWorth);
			
			$cateId = intval($cateId);
			
			$sql = "insert into dragon_fancai_vegetables(vegetable_title,cate_id,major_material,slave_material,seasoning,cooking_content,cooking_tip,relat_cnt,nutrition_worth,web_url, current_page_url) ";
			$sql .= " values('$vegetableTitle',$cateId,'$majorMaterial','$slaveMaterial','$seasoning','$cookingCnt','$cookingTip','$relatCnt','$nutritionWorth','$url', '$currentPageUrl') ";
			$html->clear();
			unset($html);
			try {
				$this->db->query($sql);
			} catch (Exception $e) {
				echo $e->getMessage();
			}
			
			
		}
		
		function insertFancaiCate($title, $parentId = 0, $oldUrl='', $orderNo=0, $isLeaf=0){
			$sql = "insert into dragon_fancai_category(cate_title,old_url,order_no,parent_id, isleaf) ";
			$sql .= " values('$title','$oldUrl',$orderNo,$parentId, $isLeaf)";
			
			$this->db->query($sql);
		} 
		
		function getParentNodeId($title){
			if (empty($title)){
				return 0;
			}
			
			$sql = "select id from dragon_fancai_category where cate_title='$title'";
			
			return intval($this->db->fetchOne($sql));
			
		}
		
		function reWriterVegetables(){
			$this->rewrite_vegetables = true;
		}
		
		function skipwhenExistsVegetables(){
			$this->rewrite_vegetables = false;
		}
		
		private function isExistsVegetables($url ='', $cateId = -1){
			$sql = "select count(1) from dragon_fancai_vegetables where web_url='$url' and cate_id=$cateId";
			$rs = $this->db->fetchOne($sql);
			return intval($rs) > 0 ? true : false;
		}
		
		private function downloadImg($vegetableId = -1, $imgUrl){
			/*
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $this->website.$imgUrl);
			echo $this->website.$imgUrl;
			$useragent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; QQDownload 1.7; TencentTraveler 4.0"; 
			curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
			curl_setopt($ch, CURLOPT_REFERER, $this->website); 
			$data = curl_exec($curl);
			
			curl_close($curl);
			
			$targetFile = "/home/dragon/workspace/publishinfo/images/vegetables".$imgUrl;
			echo $targetFile;
			$targetFile = str_replace($targetFile, "\\", "/");
			$index = strrpos($targetFile, "/");
			$dir = substr($targetFile, 0, $index);
			
			$this->create_dirs_if_not_exist($dir);
			
			$targetfp = fopen($targetFile, "a");
			fwrite($targetfp, $data);
			fclose($targetfp);
			*/
			
			ob_start();
			readfile($this->website.$imgUrl);
			$img = ob_get_contents();
			ob_end_clean();
			
			$dir = str_replace("\\", "/", $imgUrl);
			$index = strrpos($imgUrl, '/');
			$dir = substr($imgUrl, 0, $index);
			
			$this->create_dirs_if_not_exist($dir, '/home/dragon/workspace/publishinfo/images/vegetables');
			
			$targetFile = "/home/dragon/workspace/publishinfo/images/vegetables".$imgUrl;
			$targetFile = str_replace("\\", "/", $targetFile);
			$fwImg = fopen("$targetFile", "w");
			fwrite($fwImg, $img);
			fclose($fwImg);
			
			$imgUrl = addslashes($imgUrl);
			$sql = "insert into dragon_fancai_file(veget_id,w_url,v_type) values($vegetableId,'$imgUrl',1)";
			try {
				$this->db->query($sql);
			} catch (Exception $e) {
				echo $e->getMessage();
			}
			
		}
		
		private function getCurrentPage($url){
			$url = trim($url);
			if (strlen($url) <= 0) return 0; 
			$url = strtolower($url);
			/*
			$index = stripos($url, "?");
			if ($index === false || intval($index) <= 0){
				return 0;
			}
			
			$url = substr($url, ($index+1));
			if (empty($url)){
				return 0;
			}
			*/
			
			$index = stripos($url, 'page=');
			if($index === false){
				return 0;
			} 
			
			$url = substr($url, $index+5);
			if(empty($url)){
				return 0;
			}
			
			$index = stripos($url, '&');
			if($index === false){
				intval($url);
			}
			
			$url = substr($url, 0, $index);
			if(empty($url)){
				return 0;
			}
			
			return intval($url);
		}
		
	public function  create_dirs_if_not_exist($path, $prefixPath = ''){
       if(!isset($path) || strlen(trim($path)) <= 0){
            return false;
          }
          $tmpPath = '';
          $p = explode('/', $path);
          $tmpPath = "$p[0]";//"$p[0]/$p[1]";
          for($i = 1; $i < count($p); $i++){
            $tmpPath = "$tmpPath/$p[$i]";
            if(!file_exists("$prefixPath$tmpPath")){
                mkdir("$prefixPath$tmpPath");
            }
            
          }
       
    }
		
	}
	
?>